
/*
 */


package com.core.basic.secure.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import com.core.basic.secure.domain.Menu;
import com.core.basic.secure.query.MenuQuery;
import com.core.basic.secure.service.MenuManager;
import common.base.BaseRestSpringController;
import common.util.PageQueryFactory;

/**
 */

@Controller
public class MenuOperationController extends BaseRestSpringController<Menu,java.lang.Integer>{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private MenuManager menuManager;
	
	private final String LIST_ACTION = "redirect:/menu";
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setMenuManager(MenuManager manager) {
		this.menuManager = manager;
	}
	
	/** binder用于bean属性的设置 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	/**
	 * 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 根据父菜单获取子菜单 */
	@RequestMapping(value="/getMenuList",method=RequestMethod.GET)
	public String getMenuList(ModelMap model,MenuQuery query,HttpServletRequest request,HttpServletResponse response) {
		String forwardStr=request.getParameter("forward");
		if(StringUtils.isEmpty(forwardStr)){
			forwardStr="/top";
		}
		Collection<GrantedAuthority> authorities = SecurityContextHolder.getContext()
			    .getAuthentication().getAuthorities();
		PageQueryFactory.bindPageQuery(query, request,DEFAULT_SORT_COLUMNS);
		Page<Menu> page = this.menuManager.findPage(query);
		List<Menu> menus =new ArrayList<Menu>();
		
		Iterator<GrantedAuthority> it=authorities.iterator();
		while(it.hasNext()){
			GrantedAuthority grant=it.next();
			for(Menu menu:page.getResult()){
				if(grant.getAuthority().equals(menu.getResourceName())){
					menus.add(menu);
				}
			}
			
		}
		
		model.addAttribute("menus", menus);
		return forwardStr;
	}
}

